package com.qzyh.esb.egsa.utils;

import com.qzyh.esb.egsa.bean.vo.UserMenuTreeVO;
import com.qzyh.esb.egsa.entity.Menu;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;

@Mapper(componentModel = "spring")
public interface UserMenuConverter {

    // 实例
    UserMenuConverter INSTANCE = Mappers.getMapper(UserMenuConverter.class);

    /**
     * Menu 转化为 UserMenuTreeVO
     * @param menu
     * @return
     */
    @Mappings({
            @Mapping(target = "path",source = "routerPath"),
            @Mapping(target = "name",source = "routerName"),
            @Mapping(target = "component",source = "componentPath"),
            @Mapping(target = "redirect",source = "routerRedirect"),
            @Mapping(target = "meta.title",source = "menuName"),
            @Mapping(target = "meta.icon",source = "menuIcon"),
            @Mapping(target = "meta.isLink",source = "linkPath"),
            @Mapping(target = "meta.isHide",source = "menuHide",qualifiedByName = "stringToBoolean"),
            @Mapping(target = "meta.isKeepAlive",source = "menuCache",qualifiedByName = "stringToBoolean"),
            @Mapping(target = "meta.isAffix",source = "menuAffix",qualifiedByName = "stringToBoolean"),
            @Mapping(target = "meta.isIframe",source = "menuIframe",qualifiedByName = "stringToBoolean"),
    })
    UserMenuTreeVO menuToUserMenuTreeVO(Menu menu);

    // 	当执行 下面这个List的转换时，会遍历list: MenuList，
    //  然后自动调用上面的Menu转UserMenuTreeVO的转换方法，来进行转换
    /**
     * MenuList 转化为 UserMenuTreeVOList
     * @param menuList
     * @return
     */
    List<UserMenuTreeVO> menuListToUserMenuTreeVOList(List<Menu> menuList);

    @Named("stringToBoolean")
    default boolean stringToBoolean(String source) {
        if ("1".equals(source)) {
            return true;
        }
        return false;
    }

}
